package com.housekeeper.maili.housekeeper;

import android.app.Activity;
import android.app.Application;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by MAILI on 2017/9/30.
 */

public class MyApplication extends Application {

    private volatile static MyApplication application;
    private List activityList = new LinkedList();

    @Override
    public void onCreate() {
        super.onCreate();
        application=this;
    }

    private MyApplication (){}

    public static MyApplication getInstance() {
        if (application == null) {
            synchronized (MyApplication.class) {
                if (application == null) {
                    application = new MyApplication();
                }
            }
        }
        return application;
    }

    //添加Activity到容器中
    public void addActivity(Activity activity) {
//        LogUtils.wsf("acti:                "+activity+"              "+activityList);
        for (int i = 0; i < activityList.size(); i++) {
            if (activityList.get(i).getClass().getName().equals(activity.getClass().getName()))
                activityList.remove(activityList.get(i));
        }
        activityList.add(activity);
    }
    //遍历所有Activity并finish
    public void removeActivity(Activity activity) {
        for (int i = 0; i < activityList.size(); i++) {
            if (activityList.get(i).getClass().getName().equals(activity.getClass().getName()))
                activityList.remove(activityList.get(i));
        }
    }

    public void exit() {
        for (Object activity : activityList) {
            ((Activity) activity).finish();
        }
        activityList = null;
        System.exit(0);
        // 强制让应用退出
//        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
